package com.fx.oss;

import com.fx.client.FolderType;
import com.fx.client.MagicOssClient;
import com.fx.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/oss")
public class OssController extends BaseController{
    @Autowired
    private MagicOssClient magicOssClient;

    @RequestMapping("/put")
    public Object upload(@RequestParam("file") MultipartFile file, String fix) {
        String url = "";
        try {
            url = magicOssClient.upload("upms", "userAvatar", FolderType.yyyyMM, file.getInputStream(),fix);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responseSuccess(url);
    }

    @RequestMapping("/upload/{biz}")
    public Object upload(@PathVariable String biz, @RequestParam("file") MultipartFile file) {
        String url = "";
        try {
            String originalFilename = file.getOriginalFilename();
            url = magicOssClient.upload("fxoa", biz, FolderType.yyyyMMdd, file.getInputStream(), originalFilename.substring(originalFilename.lastIndexOf(".")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotEmpty(url)) {
            return responseSuccess(url);
        } else {
            return responseFail("上传至oss失败");
        }
    }
}
